package com.northchurchcolumbus.podcast.ncpodcaster;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class NCPodcasterTerminal
{

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
        /* Envision the following vars as preferences */
        String podcastInput=null;

        String urlPrefix="http://ecbiz94.inmotionhosting.com/~northc22/podcast/content/";
        String testBloggerBlogId="8422928620011639430";
        String bloggerBlogId="7353461746210490089";
        URL artworkFileURL=NCPodcasterTerminal.class.getClass().getResource("/Picture.jpeg");

        InputStream is =artworkFileURL.openStream();
        File artworkFile = File.createTempFile("artwork", "jpeg");
        artworkFile.deleteOnExit();
        OutputStream artworkFileOutput = new FileOutputStream(artworkFile);
        byte[] bytes = new byte[1024];
        int bread = -1;
        while((bread=is.read(bytes))>=0)
        {
            artworkFileOutput.write(bytes, 0, bread);
        }
        artworkFileOutput.close();
        is.close();
        
        /* Envision the following vars as user input */
        Calendar now = Calendar.getInstance();
        int month= now.get(Calendar.MONTH)+1;
        int day=now.get(Calendar.DAY_OF_MONTH);
        int year=now.get(Calendar.YEAR);
        String title="By Design Series";
        String speaker="Mike Borst";

        
        Map<String, String> ctx = new HashMap<String,String>();
        ctx.put("ftp_server","ecbiz94.inmotionhosting.com");
        ctx.put("ftp_username","podcast@northchurchcolumbus.com");
        ctx.put("ftp_password","KAMKOBkm2fLy");
        
        ctx.put("blog_username","brett.humphreys@gmail.com");
        ctx.put("blog_password","024816");
        ctx.put("blog_id", testBloggerBlogId);
        ctx.put("blogger_appid", "northchurchcolumbus-ncpodcaster-100");

        ctx.put("urlPrefix", urlPrefix);

        for(int argIdx=0; argIdx<args.length; argIdx++)
        {
            String arg=args[argIdx];
            if(arg.equals("-?") || arg.equals("-help") || arg.equals("-h"))
            {
                printUsageAndExit();
            }
            else if(arg.equals("-f"))
            {
                podcastInput=args[++argIdx];
            }
            else if(arg.equals("-t"))
            {
                title=args[++argIdx];
            }
            else if(arg.equals("-s"))
            {
                speaker=args[++argIdx];
            }
            else if(arg.equals("-m"))
            {
                month=Integer.parseInt(args[++argIdx]);
            }
            else if(arg.equals("-d"))
            {
                day=Integer.parseInt(args[++argIdx]);
            }
            else if(arg.equals("-y"))
            {
                year=Integer.parseInt(args[++argIdx]);
            }
            else if(arg.equals("--pub"))
            {
                ctx.put("blog_username","northchurchcolumbus.com@gmail.com");
                ctx.put("blog_password","01172010");
                ctx.put("blog_id", bloggerBlogId);
            }
            else
            {
                printUsageAndExit();
            }
        }
        
        
        System.out.println("Input File: " + podcastInput);
        System.out.println("Title: " + title);
        System.out.println("Speaker: " + speaker);
        System.out.println("Month: " + month);
        System.out.println("Day: " + day);
        System.out.println("Year: " + year);
        for(Map.Entry<String, String> e: ctx.entrySet())
        {
            System.out.println("CTX " + e.getKey() + " : " + e.getValue());
        }
        
        if(podcastInput==null)
        {
            System.out.println("Error: no input file set");
            printUsageAndExit();
        }
        if(title==null)
        {
            System.out.println("Error: no title set");
            printUsageAndExit();
        }        
        if(speaker==null)
        {
            System.out.println("Error: no speaker set");
            printUsageAndExit();
        }
        
        NCPodcaster podcaster = new NCPodcaster(ctx, new File(podcastInput), month, day, year, title, speaker);
        podcaster.encode();
        podcaster.tag();
        podcaster.upload(null, false);
        podcaster.publish(true);
    }

    public static void printUsageAndExit()
    {
        System.out.println("Usage:");
        System.out.println("podcaster [options]");
        System.out.println("Options:");
        System.out.println("\t-t title");
        System.out.println("\t-s speaker");
        System.out.println("\t-f filename");
        System.out.println("\t-m month (numeric)");
        System.out.println("\t-d day (numeric)");
        System.out.println("\t-y year (numeric)");
        System.out.println("\t--pub : publishes this post");
        System.exit(1);
    }
    
}
